package org.kev.transit.util;



import org.kev.transit.R;
import org.kev.transit.SettingsActivity;
import org.kev.transit.data.TimeTable;
import org.kev.transit.data.RouteType;
import org.kev.transit.data.Route;
import org.kev.transit.data.Stop;
import org.kev.transit.data.Direction;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

/**
 * create test data file for the application
 * I will someday remove this class
 * 
 * @author kscott
 * 
 */
public class TestDataHelper {
	private static final String TAG = "TestDataHelper";
	private SettingsActivity parent = null;
	// private StringBuffer buffer = new StringBuffer();


	private Runnable mProgressCallback;
	private Handler mHandler = null;

	public TestDataHelper(SettingsActivity updateWorker, Handler handler,
			Runnable progressCallback) {
		parent = updateWorker;

		mProgressCallback = progressCallback;
		mHandler = handler;
	}

	public TestDataHelper(SettingsActivity activity){
		parent = activity;


	}
	
	/**
	 * make test data file and insert data into new DB
	 * 
	 * 
	 */
	public void getTestContent() {
		try {
			Log.w(TAG, "etTestContent()");
			
			
			//TranistProvider.deleteData();
			//delete cached data
			parent.getContentResolver().delete(RouteType.CONTENT_URI,null,null);
			parent.getContentResolver().delete(Route.CONTENT_URI,null,null);
			parent.getContentResolver().delete(Direction.CONTENT_URI,null,null);
			parent.getContentResolver().delete(Stop.CONTENT_URI,null,null);
			parent.getContentResolver().delete(TimeTable.CONTENT_URI,null,null);
			//parent.createDatabase(TransitConst.DATA_BASE, 3,Context.MODE_WORLD_READABLE, null);


//			Log.d(TAG, "Created DB=" + TransitConst.DATA_BASE);
			String[] routeTypes = parent.getResources().getStringArray(
					R.array.routeTypes);
			String[] directions = parent.getResources().getStringArray(
					R.array.direction);
			String[] routes = parent.getResources().getStringArray(
					R.array.routes);
			String[] stops = parent.getResources()
					.getStringArray(R.array.stops);
			String[] times = parent.getResources()
					.getStringArray(R.array.times);
			String[] lats = parent.getResources().getStringArray(
					R.array.stopLat);
			String[] lons = parent.getResources().getStringArray(
					R.array.stopLong);

			int count = 0;
			for (int rt=0 ;rt<routeTypes.length;rt++){
				//insertRouteType("" + (rt+1) , routeTypes[rt]);
				for (int r=0 ;r<routes.length;r++){
					//insertRoute(""+(r+1) ,""+(rt+1), routes[r]);
					for(int d=0;d<directions.length;d++){
						//insertDirection(""+(d+1), ""+(rt+1),""+(r+1),directions[d]);
					}
				}
			}
			
			for (int rt = 0; rt < routeTypes.length; rt++) {

				for (int r = 0; r < routes.length; r++) {

					for (int d = 0; d < directions.length; d++) {

						for (int s = 0; s < stops.length; s++) {
							//insertStop(""+s+1,stops[s],lats[s],lons[s],""+rt+1,""+(r+1), ""+(d+1));
							for (int t = 0; t < times[t].length(); t++) {
								count++;
								Log.d(TAG, "insert row" + count);
								//insertTime(""+(rt+1), ""+(r+1), ""+(d+1),""+(s+1), times[t]);

							} //end t time loop
						} //end stop s loop
					} // end d direction loop
				}// r route

			} //end i loop(route type

		} catch (Exception e) {// log and ignore!
			Log.e(TransitConst.TAG, "error creating Test data TestDateHelper",
					e);
		}
	}

	/**
	 * 
	 * @param id
	 * @param title
	 */
	private Uri insertRoute(String id, String routeType, String title) {
		Log.d(TAG, "insertRoute"+id+routeType+title);
		ContentValues values = new ContentValues();
		
		values.put(Route.ROUTE_ID, new Integer(id));
		values.put(Route.ROUTE_TYPE, new Integer(routeType) );
		values.put(Route.TITLE, title);
		Uri curl = parent.getContentResolver().insert(Route.CONTENT_URI,
				values);
		return curl;
	}

	/**
	 * 
	 * @param id
	 *            data field 2
	 * @param title
	 *            field 3
	 */
	private Uri insertRouteType(String id, String title) {
		if (mHandler != null && mProgressCallback != null)
			mHandler.post(mProgressCallback);
		Log.d(TAG, "insertRouteType");
		ContentValues values = new ContentValues();
		values.put(RouteType.ROUTE_TYPE_ID, new Integer(id));
		values.put(RouteType.TITLE, title);
		Log.d(TAG,"time="+System.currentTimeMillis());
		values.put(RouteType.CREATE_DATE,new Long(System.currentTimeMillis() ) );
		Uri curl = parent.getContentResolver().insert(RouteType.CONTENT_URI,	values);
		return curl;
	}

	/**
	 * 
	 * @param id
	 * @param routeType
	 * @param route
	 * @param direction
	 * @param title
	 */
	private Uri insertDirection(String id, String routeType, String route,
			String title) {
		if (mHandler != null && mProgressCallback != null)
			mHandler.post(mProgressCallback);
		Log.d(TAG, "+++nsertDirection");
		ContentValues values = new ContentValues();
		values.put(Direction.DIRECTION_ID, id);
		values.put(Direction.ROUTE_TYPE, routeType);
		values.put(Direction.ROUTE, route);
		values.put(Direction.TITLE, title);
		values.put(Direction.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		Uri curl = parent.getContentResolver().insert(Direction.CONTENT_URI,
				values);
		return curl;
	}
	/**
	 * 
	 * @param id
	 * @param title
	 * @param lat
	 * @param lon
	 * @return
	 */
	private Uri insertStop(String id, String title, String lat, String lon,String rtype, String route,String dir){
		if (mHandler != null && mProgressCallback != null)
			mHandler.post(mProgressCallback);
		Log.d(TAG, "+++insertStop");
		ContentValues values = new ContentValues();
		values.put(Stop.STOP_ID, id);
		values.put(Stop.TITLE, title);
		values.put(Stop.LAT, lat);
		values.put(Stop.LON, lon);
		values.put(Stop.ROUTE_TYPE,rtype);
		values.put(Stop.ROUTE,route);
		values.put(Stop.DIRECTION,dir);
		values.put(Stop.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		Uri curl = parent.getContentResolver().insert(Stop.CONTENT_URI,values);
		return curl;
	}
	

	/**
	 * 
	 * 
	 * @param routeType
	 * @param route
	 * @param direction
	 * @param stopId
	 * @param time
	 * @return
	 */
	private Uri insertTime(String routeType, String route, String direction,
			String stopId, String time) {
		Log.d(TAG, "insertDB {TIME_TABLE}");
		if (mHandler != null && mProgressCallback != null)
			mHandler.post(mProgressCallback);
		ContentValues values = new ContentValues();
		// values.put(TimeTable.ID, id);
		//values.put(TimeTable.TIME_ID, timeId);
		values.put(TimeTable.ROUTE_TYPE, routeType);
		values.put(TimeTable.ROUTE, route);
		values.put(TimeTable.DIRECTION, direction);
		values.put(TimeTable.STOP_ID, stopId);
		values.put(TimeTable.TIME, time);
		values.put(TimeTable.MODIFIED,new  Long(System.currentTimeMillis() ) );
		// intent.s
		Log.d(TAG, "parent=" + parent);
		Log.d(TAG, "res=" + parent.getContentResolver());
		Log.d(TAG, "res=" + TimeTable.CONTENT_URI);
		Log.d(TAG, "values=" + values);
		Uri curl = parent.getContentResolver().insert(
				TimeTable.CONTENT_URI, values);
		return curl;
	}

}
